package org.extframework.ld.http.abstr;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.extframework.ld.http.EncryptionWebRequest;
import org.extframework.ld.http.EncryptionWebResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @author : Dong.Li
 * @created : 2013-1-30
 * @version : 1.0
 * @description :
 */
public abstract class AbstractEncryptionWebFilter implements Filter {

	private ApplicationContext applicationContext;

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain fc) throws IOException, ServletException {
		AbstractEncryptionWebRequest request = new EncryptionWebRequest(
				(HttpServletRequest) req);
		AbstractEncryptionWebResponse response = new EncryptionWebResponse(
				(HttpServletResponse) resp);
		if (this.applicationContext == null) {
			this.applicationContext = WebApplicationContextUtils
					.getWebApplicationContext(request.getSession()
							.getServletContext());
		}
		doFilter(request, response, fc);
	}

	public abstract void doFilter(
			AbstractEncryptionWebRequest paramAbstractEncryptionWebRequest,
			AbstractEncryptionWebResponse paramAbstractEncryptionWebResponse,
			FilterChain paramFilterChain);

	protected <T> T getBean(Class<T> paramClass) {
		return this.applicationContext.getBean(paramClass);
	}

	protected Object getBean(String beanId) {
		return this.applicationContext.getBean(beanId);
	}

	public void init(FilterConfig arg0) throws ServletException {}

}
